package br.com.werp.suporte.Usuario;

import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import com.egen.util.jdbc.*;
import com.egen.util.text.*;

/**
 * Creation Date 03/09/2004 10:24:40
 * Last Modify Date 23/02/2006 16:10:02
 */

public class EmailAction extends com.egen.util.struts.AbstractAction {

  public ActionForward perform_select_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    com.egen.util.jdbc.JdbcUtil j = null;
    try {
      j = new com.egen.util.jdbc.JdbcUtil();
      select_action(j, mapping, form, request, response);
      actionForward = mapping.findForward("same");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              (e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          (e));
      actionForward = mapping.findForward("same");
    }
    finally { if (j != null) {
          j.close();
        }
            } return actionForward;
  }
  private void select_action(JdbcUtil j, ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) throws Exception {
    HttpSession session = request.getSession(true);
    EmailActionForm f = (EmailActionForm) form;
    br.com.werp.suporte.dbobj.table.Email table =
      new br.com.werp.suporte.dbobj.table.Email();
    String[][] select = null;
    String[] order = null;
    String[] groupby = null;
    String[] having = null;
    Object[][] where = { {"Emil_endereco","like", f.getEmil_endereco()} };
    Vector res_Email = j.select(table, select, where, groupby, having, order);
    session.setAttribute("res_Email",res_Email);
    if (res_Email != null && res_Email.size() > 0) {
      br.com.werp.suporte.dbobj.table.Email r =
        (br.com.werp.suporte.dbobj.table.Email)
        res_Email.elementAt(0);
      f.setEmil_endereco((r.getEmil_endereco() != null) ?
          r.getEmil_endereco() + "":"");
    } else {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("warn.norowsselected",""));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
    }
    session.setAttribute(mapping.getName(), form);
  }

  public ActionForward perform_insert_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    com.egen.util.jdbc.JdbcUtil j = null;
    try {
      j = new com.egen.util.jdbc.JdbcUtil();
      insert_action(j, mapping, form, request, response);
      actionForward = mapping.findForward("same");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              (e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          (e));
      actionForward = mapping.findForward("same");
    }
    finally { if (j != null) {
          j.close();
        }
            } return actionForward;
  }
  private void insert_action(JdbcUtil j, ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) throws Exception {
    HttpSession session = request.getSession(true);
    EmailActionForm f = (EmailActionForm) form;
    br.com.werp.suporte.dbobj.table.Email table =
      new br.com.werp.suporte.dbobj.table.Email();
    Object[][] name_value = { {"Emil_endereco",f.getEmil_endereco()}, };
    //###############################auditoria.INCLUIR#########################
    try {
      j.setAutoCommit(false);
      j.insert(table, name_value);
      br.com.werp.regras.geral.Auditoria auditoria =
        br.com.werp.regras.geral.FabricaSubSistema.getAuditoria();
      String usuario = (String) session.getAttribute("j_username");
      auditoria.auditarRecurso(j.getConn(), usuario, table,
          auditoria.INCLUIR);
      j.commit();
    } catch (Exception e) {
      j.rollback();
      throw e;
    }
    finally{ j.setAutoCommit(true);
           } //###############################auditoria.INCLUIR#########################
    session.setAttribute("EmailActionForm",new EmailActionForm());
    Vector res_Email = (Vector) session.getAttribute("res_Email");
    if (res_Email != null && table != null) {
      res_Email.addElement(table);
      session.setAttribute("res_Email", res_Email);
    }
  }

  public ActionForward perform_resetfull_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    try {
      session.removeAttribute("res_Email");
      form.reset(mapping, request);
      session.setAttribute(mapping.getName(), form);
      actionForward = mapping.findForward("same");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              (e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          (e));
      actionForward = mapping.findForward("same");
    }
    return actionForward;
  }

}
